package dorm_admin_sys;

import java.sql.*;
import java.util.Objects;

public class register_sql {
        //jdbc新版本的设备驱动和连接字符串
        private static String driver="com.mysql.cj.jdbc.Driver";
        private static String url="jdbc:mysql://localhost:3306/dorm_admin_sys?characterEncoding=utf-8&serverTimezone=UTC";

        private static String user="root";
        private static String passwd="mysql";
        public boolean resultRegisterSqlStatus(String registerUser, String registerpassword)throws ClassNotFoundException {

            boolean connStatus=false;//返回给前台页面，判断连接是否成功

            //System.out.println("-----------------连接数据库-----------------");
            //Scanner in_user=new Scanner(System.in);
            //System.out.print("输入用户名：");
            //user=new Scanner(System.in).next();
            //System.out.print("输入密码：");
            //passwd=new Scanner(System.in).next();
            //System.out.println("user:"+user+"   passwd:"+passwd);
            String sql="select * from admin;";
            try (   //建立驱动程序与数据库之间的连接
                    Connection conn= DriverManager.getConnection(url,user,passwd);
                    //利用连接对象conn创建Statement接口对象
                    Statement stmt= conn.createStatement();
                    ResultSet rs=stmt.executeQuery(sql)
            ){
                Class.forName(driver);//加载驱动程序
                //System.out.println("学号       姓名    性别   年龄     系别    政治面貌");
                System.out.println("用户名   密码");
                while (rs.next()){
                    /*
                    String no=rs.getString("SNo");
                    String name=rs.getString("SName");
                    String sex=rs.getString("SSex");
                    Short age=rs.getShort("SAge");
                    String dept=rs.getString("SDept");
                    String zzmm=rs.getString("szzmm");
                     */

                    String get_user=rs.getString("user");
                    String get_passwd=rs.getString("passwd");
                    System.out.println(get_user+"  "+get_passwd);
                    //if((Objects.equals(registerUser, get_user) && Objects.equals(registerpassword, get_passwd))) {
                    if (registerUser.equals(get_user)&&registerpassword.equals(get_passwd)){
                        connStatus = true;
                    }
                    else
                        connStatus=false;
                    //System.out.println(no+" | "+name+" | "+sex+" | "+age+" | "+dept+" | "+zzmm);
                }
            }
            catch (SQLException sqle){
                sqle.printStackTrace();//输出当前异常对象的堆栈使用痕迹
            }

            return connStatus;
        }
}


